package com.diandiansong;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.diandiansong.app.App;
import com.diandiansong.app.ui.user.Login;

import java.util.List;

import diandiansong.library.ui.base.BaseAct;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 1)	AndroidManifest.xml 文件注释
 * 2)	app下面build.gradle文件targetSdkVersion 必须大于等于27，说明申请权限在那个位置（文件权限、定位权限、拍照权限等）
 * 3)	建议统一implementation，不要使用即将过时compile，library包也一样需要调整
 * 4)	 minifyEnabled true代码混淆必须开启，代码混淆规则必须要有，library包不要求混淆
 * 5)	移除支付宝等相关demo文件或修改
 * 6)	每个java文件需要有说明
 * 7)	方法注释、关键变量有说明
 */

/**
 * 启动页
 * 根据包名判断启动用户端或主管端
 */
public class Splash extends BaseAct implements EasyPermissions.PermissionCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideTitle();

        String[] perms = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CAMERA
        };
        //
        if (EasyPermissions.hasPermissions(this, perms)) {
            // 已经申请过权限，做想做的事
            goOn();

        } else {
            // 没有申请过权限，现在去申请
            //权限申请
            EasyPermissions.requestPermissions(
                    this,
                    "申请权限",
                    0,
                    perms
            );
        }


    }

    private void goOn() {
        if (App.isUserClient()) {
            //用户端
            Login.start(getActivity());

        } else {
            //主管端
            com.diandiansong.manager.ui.user.Login.start(getActivity());
        }
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override //申请成功时调用
    public void onPermissionsGranted(int requestCode, List<String> list) {
        //请求成功执行相应的操作
        logger.d("onPermissionsGranted() called with: " + "requestCode = [" + requestCode + "], list = [" + list + "]");
        switch (requestCode) {
            case 0:
                goOn();
                break;
        }
    }

    @Override //申请失败时调用
    public void onPermissionsDenied(int requestCode, List<String> list) {
        // 请求失败，执行相应操作
        toast("请赋予程序必要的权限");
        finish();
    }
}
